package com.megvii.koala.data.filter;

/**
 * Created by linyue on 15/12/31.
 */
public abstract class CharacterFilter<T> implements ObjectFilter<T>
{
    protected String filterStr;

    protected boolean whenFilterStrNull;

    public CharacterFilter(String filterStr, boolean whenFilterStrNull)
    {
        super();
        if (filterStr != null)
        {
            filterStr = filterStr.trim().toLowerCase();
            if (filterStr.length() == 0)
            {
                this.filterStr = null;
            }
            else
            {
                this.filterStr = filterStr;
            }
        }
        this.whenFilterStrNull = whenFilterStrNull;
    }

    @Override
    public boolean filter(T object)
    {
        if (filterStr == null)
        {
            return whenFilterStrNull;
        }
        else
        {
            String value = getValueToFilter(object);
            String phoneticizeValue = CharacterParser.getInstance().getSelling(value);
            if (value.toLowerCase().indexOf(filterStr) >= 0 || phoneticizeValue
                    .startsWith(filterStr))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    abstract public String getValueToFilter(T object);
}
